import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ReadXml {
    private String xmlPath = "";
    public ReadXml() {}
    public ReadXml(String xmlPath) {
        this.xmlPath = xmlPath;
    }
    public List<String> readCarNameByXml(){
        List<String> carNameList = new ArrayList<String>();
        try{
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new File(this.getXmlPath()));
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("carName");
            carNameList.add(nList.item(0).getFirstChild().getNodeValue());
            carNameList.add(nList.item(1).getFirstChild().getNodeValue());
            carNameList.add(nList.item(2).getFirstChild().getNodeValue());
        }catch(Exception e){
            e.printStackTrace();
        }
        return carNameList;
    }

    public String getXmlPath() {
        return xmlPath;
    }

    public void setXmlPath(String xmlPath) {
        this.xmlPath = xmlPath;
    }
}
